//





//
//  newcollection.swift
//  GZWeibo05
//
//  Created by myd on 15/12/17.
//  Copyright © 2015年 zhangping. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class newcollection: UICollectionViewController {
  let num = 4
   var lay = UICollectionViewFlowLayout()
    init()
    {
  super.init(collectionViewLayout: lay)
      
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func coller()
    {
        self.collectionView?.registerClass(newcell.self, forCellWithReuseIdentifier: "newcell")
        lay.itemSize=UIScreen.mainScreen().bounds.size
        lay.minimumInteritemSpacing=0
        lay.minimumLineSpacing=0
        lay.collectionView?.pagingEnabled=true
        lay.collectionView?.bounces=false
        lay.scrollDirection=UICollectionViewScrollDirection.Horizontal
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor=UIColor.whiteColor()
           coller()
    }
 

//    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return 1
//    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return num
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("newcell", forIndexPath: indexPath) as! newcell
//        cell.backgroundColor=UIColor.orangeColor()
        // Configure the cell
       cell.index = indexPath.item
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let ind = collectionView.indexPathsForVisibleItems().last!
       if ind.item == num-1
       {
       let ce = collectionView.cellForItemAtIndexPath(ind) as! newcell
        ce.start()
        }
    }
}




class  newcell : UICollectionViewCell
{
    var index = 0
        {
   willSet
   {

        }didSet
        {
           image.image=(UIImage(named: "new_feature_\(index+1)"))
            btn.hidden=true
        }
    }
    lazy var image : UIImageView = {
    let ima = UIImageView()
        return ima
    }()
    
    lazy var btn : UIButton = {
   let bb = UIButton()
        bb.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        bb.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        bb.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
    bb.setTitle("登陆微博", forState: UIControlState.Normal)
        return bb
    }()
    
   override init(frame: CGRect) {
       super.init(frame: frame)
    reldayui()
    
    }
    func login()
    {
        
   let window = UIApplication.sharedApplication().keyWindow
        window?.loadnowvrdion()
        window?.login()
    }
   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    func start()
    {
        btn.hidden=false
        btn.transform=CGAffineTransformMakeScale(0, 0)
        
        UIView .animateWithDuration(1, delay: 0, usingSpringWithDamping:0.5, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
               self.btn.transform=CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
     
    }
 func reldayui()
 {
   self.contentView.addSubview(image)
    self.contentView.addSubview(btn)
    image.translatesAutoresizingMaskIntoConstraints=false
    btn.translatesAutoresizingMaskIntoConstraints=false
    
    
    self.addConstraint(NSLayoutConstraint(item: image, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: image, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: image, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: image, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
    
     self.addConstraint(NSLayoutConstraint(item: btn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
     self.addConstraint(NSLayoutConstraint(item: btn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant:-100))
    }
    }
